<table>
    <tr>
        <td colspan="4">
            <?php echo $this->element('excel/title'); ?>
        </td>
        <td colspan="4">
            <table>
                <tr>
                    <td colspan="4"  class="docNote">VMI REPORT: Customer Stock in</td>
                </tr>
                <tr>
                    <td class="label">Report date</td>
                    <td><?php echo $report_date; ?></td>
                    <td class="label">Report by</td>
                    <td><?php echo $user; ?></td>
                </tr>
                <tr>
                    <td width="100px" class="label">Customer no.</td>
                    <td colspan="3"><?php echo $customer_no; ?></td>
                </tr>
                <tr>
                    <td class="label">Customer name</td>
                    <td colspan="3"><?php echo $customer_name; ?></td>
                </tr>
                <tr>
                    <td class="label">Site</td>
                    <td colspan="5">
                        <?php echo $site_no; ?>&nbsp;
                        <?php echo $site_name; ?>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<table>
    <?php if (!empty($criteria['CustProductSite']['stock_code'])) : ?>
    <tr>
        <td width="90px" class="label">Stock Code</td>
        <td><?php echo $criteria['CustProductSite']['stock_code']; ?></td>
    </tr>
    <?php endif; ?>
    <?php if (!empty($criteria['Product']['category_id'])) : ?>
    <tr>
        <td width="90px" class="label">Category</td>
        <td><?php echo $category_name; ?></td>
    </tr>
    <?php endif; ?>
    <?php if (!empty($criteria['Product']['brand_id'])) : ?>
    <tr>
        <td width="90px" class="label">Brand</td>
        <td><?php echo $brand_name; ?></td>
    </tr>
    <?php endif; ?>
    <?php if (!empty($criteria['CustStockInHead']['dispatch_from'])) : ?>
    <tr>
        <td width="90px" class="label">Vendor</td>
        <td><?php echo $vendor; ?></td>
    </tr>
    <?php endif; ?>
    <?php if (!empty($criteria['CustStockInHead']['stock_in_date_start'])) : ?>
    <tr>
        <td width="90px" class="label">Stock in date</td>
        <td>From <?php echo $criteria['CustStockInHead']['stock_in_date_start']; ?> to <?php echo $criteria['CustStockInHead']['stock_in_date_end']; ?></td>
    </tr>
    <?php endif; ?>
    <tr>
        <td width="90px" class="label">Order by</td>
        <td>
            <?php echo !empty($criteria['Option']['order1'])? $orderOptions[$criteria['Option']['order1']]: ''; ?>
            <?php echo !empty($criteria['Option']['order2'])? $orderOptions[$criteria['Option']['order2']]: ''; ?>
            <?php echo !empty($criteria['Option']['seq']) && $criteria['Option']['seq'] == 'desc'? ' Descendent': ' Ascendent'; ?>
        </td>
    </tr>
    <?php if (!empty($criteria['Option']['limit'])) : ?>
    <tr>
        <td width="90px" class="label">Show</td>
        <td><?php echo $criteria['Option']['limit']; ?> entries</td>
    </tr>
    <?php endif; ?>
</table>

<!--div class="header">
</div-->

<div class="main">
    <table id="dataTable" class="dataTable" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th class="no">No.</th>
                <th width="<?php echo ($display_stock_in_no == true)? '150': '100';?>px">Stock code <?php if ($display_stock_in_no == true) echo '/ <br /> Stock in no.'; ?></th>
                <th width="<?php echo ($display_stock_in_no == true)? '140': '130';?>px">Vendor Part no.<?php if ($display_stock_in_no == true) echo '/ <br /> Stock in date'; ?></th>
                <th>Vendor <?php if ($display_stock_in_no == true) echo '/ <br /> Product Desription'; ?></th>
                <th width="40px">Freq.</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <th width="45px">Price</th>
                <?php endif; ?>
                <th width="40px">Qty.</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <th width="50px">Amount</th>
                <?php endif; ?>
            </tr>
        </thead>
        <tbody>
            <?php $i = 0;
            $r = 0;
            $total_freq = 0;
            $total_qty = 0;
            $total_amount = 0;
            foreach($this->data as $i => $data): ?>
            <tr>
                <td class="center"><?php echo ($i + 1); ?></td>
                <td class="<?php echo $display_stock_in_no == true? 'bold': ''; ?>">
                        <?php echo $data['CustProductSite']['stock_code']; ?>
                </td>
                <td class="<?php echo $display_stock_in_no == true? 'bold': ''; ?>">
                        <?php echo $data['CustStockInHead'][0]['dispatch_from'] == ITS_PREFIX? $data['Product']['part_no']: $data['Product']['supplier_part_no']; ?>
                </td>
                <td class="<?php echo $display_stock_in_no == true? 'bold': ''; ?>">
                        <?php echo $data['CustStockInHead'][0]['dispatch_from'] == ITS_PREFIX?
                        $this->requestAction('system_preferences/get/company_name'): $data['Supplier']['name']; ?>
                </td>
                <td class="text-right<?php echo $display_stock_in_no == true? 'bold': ''; ?>"><?php echo $data[0]['frequency']; ?></td>
                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right<?php echo $display_stock_in_no == true? 'bold': ''; ?>"><?php echo number_format($data['CustStockIn']['price'], 2); ?></td>
                    <?php endif; ?>
                <td class="text-right<?php echo $display_stock_in_no == true? 'bold': ''; ?>"><?php echo $data[0]['stock_in_qty']; ?></td>
                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right<?php echo $display_stock_in_no == true? 'bold': ''; ?>"><?php echo number_format($data['CustStockIn']['price'] * $data[0]['stock_in_qty'], 2); ?></td>
                    <?php endif; ?>
                    <?php
                    $total_freq += $data[0]['frequency'];
                    $total_qty += $data[0]['stock_in_qty'];
                    $total_amount += $data['CustStockIn']['price'] * $data[0]['stock_in_qty'];
                    ?>
            </tr>
                <?php if ($display_stock_in_no == true) :
                    // ตัวแปรสำหรับลำดับเลข stock in no
                    $k = 1;
                    foreach($data['CustStockInHead'] as $sto) :
                        ?>
            <tr>
                <td class="rowTd">&nbsp;</td>
                            <?php if ($display_stock_in_no == true) : ?>
                <td><span><?php echo $sto['stock_in_no']; ?></span></td>
                <td><?php echo $sto['CustStockIn']['stock_in_date']; ?></td>
                <td><?php echo $k == 1? $data[0]['text']: '&nbsp;'; ?></td>
                <td>&nbsp;</td>
                                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td>&nbsp;</td>
                                <?php endif; ?>
                <td class="text-right"><?php echo $sto['CustStockIn']['stock_in_qty']; ?></td>
                                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($sto['CustStockIn']['stock_in_qty'] * $data['CustStockIn']['price'], 2); ?></td>
                                <?php endif; ?>
                            <?php endif; ?>
            </tr>
                        <?php ++$k;
                    endforeach; ?>
                <?php endif; ?>
            <?php
            endforeach; ?>
            <tr class="trFooter">
                <td colspan="4" class="text-right">Grand Total</td>
                <td class="text-right"><?php echo number_format($total_freq); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right">-</td>
                <?php endif; ?>
                <td class="text-right"><?php echo number_format($total_qty); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($total_amount, 2); ?></td>
                <?php endif; ?>
            </tr>
        </tbody>
    </table>
</div>

<div class="footer"></div>

<div id="divLastFooter"></div>